package com.daidai.controller;

import org.springframework.ui.Model;
import com.daidai.enums.UserType;
import com.daidai.model.User;
import com.daidai.service.impl.ActivityServiceImpl;
import com.daidai.service.impl.AssociationServiceImpl;
import com.daidai.service.impl.NewsServiceImpl;
import com.daidai.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 17:47
 * Description:
 */
@Controller
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private NewsServiceImpl newsService;
    @Autowired
    private AssociationServiceImpl associationService;
    @Autowired
    private ActivityServiceImpl activityService;


    @GetMapping("/")
    public String toLogin() {
        return "mid/login";
    }

    @PostMapping("/login")
    public String checkLogin(User user, HttpSession httpSession, HttpServletRequest httpServletRequest) {
        User validUser = userService.validUser(user.getUserId(), user.getPassword());
        if (validUser == null) {
            httpServletRequest.setAttribute("msg", user.getUserId());
            return "mid/login";
        } else {
            httpSession.setAttribute("userId", user.getUserId());
            httpSession.setAttribute("userType", userService.findUserTypeById(user.getUserId()));
            return "redirect:associations";
        }
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String register(User user, HttpSession httpSession, HttpServletRequest httpServletRequest) {
        User validUserAccount = userService.validUserAccount(user.getUserId());

        if (validUserAccount != null) {//账户已存在
            httpServletRequest.setAttribute("msg", "用户已存在");
            return "register";
        } else {//进行注册
            userService.addUser(user.getUserId(), user.getPassword(), UserType.STUDENT.getUserType());
            httpSession.setAttribute("userId", user.getUserId());
            httpSession.setAttribute("userType", userService.findUserTypeById(user.getUserId()));
            return "redirect:associations";
        }
    }

    @RequestMapping("/exit")
    public String exit(HttpSession session, HttpServletRequest httpServletRequest, Model model, @RequestParam(name = "type", required = false, defaultValue = "1") int type) {

        httpServletRequest.setAttribute("type", type);
        httpServletRequest.setAttribute("associationsList", associationService.findAssociationsByType(type));

        model.addAttribute("signatureActivity", "**大赛");

        httpServletRequest.setAttribute("lastNews", newsService.findLastNews());
        httpServletRequest.setAttribute("lastActivity", activityService.findLastActivity());

        session.invalidate();

        return "mid/index";
    }
}












